package com.digital.herbarium.Controller;

import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.Entity.Stat;
import com.digital.herbarium.Util.ArrUtil;
import com.digital.herbarium.Util.TimeUtil;
import com.digital.herbarium.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("stat")
public class StatController {

    @Autowired
    DnaService dnaService;
    @Autowired
    AtlasService atlasService;
    @Autowired
    SpecimenService specimenService;
    @Autowired
    ImageService imageService;
    @Autowired
    PopularService popularService;
    @Autowired
    LoggerService loggerService;

    @GetMapping("getStatDna")
    public ResponseResult getStatDna(){
        return ResponseResult.SUCCESS().setData( dnaService.getStatGroupByType());
    }


    @GetMapping("getStatAtlas")
    public ResponseResult getStatAtlas(){
        return ResponseResult.SUCCESS().setData( atlasService.getStatGroupByType());
    }

    @GetMapping("getStatSpecimen")
    public ResponseResult getStatSpecimen(){
        return ResponseResult.SUCCESS().setData( specimenService.getStatGroupByType());
    }

    @GetMapping("getStatPlantImage")
    public ResponseResult getStatPlantImage(){
        return ResponseResult.SUCCESS().setData( imageService.getStatGroupByType());
    }

    @GetMapping("getStatTotal")
    public ResponseResult getStatTotal(){
        HashMap<String,Integer> map = new HashMap<>();
        // 标本数
        Integer stotal = specimenService.getTotal();
        Integer sytotal = specimenService.getYesterdayTotal();
        Integer snowtotal = specimenService.getNowTotal();
        // 原植物图库
        Integer itotal = imageService.getTotal();
        Integer iytotal = imageService.getYesterdayTotal();
        Integer intotal = imageService.getNowTotal();
        // 图谱
        Integer atotal = atlasService.getTotal();
        Integer aytotal = atlasService.getYesterdayTotal();
        Integer antotal = atlasService.getNowTotal();
        // dna
        Integer dtotal = dnaService.getTotal();
        Integer dytotal = dnaService.getYesterdayTotal();
        Integer dntotal = dnaService.getNowTotal();
        // 文章数量
        Integer ptotal = popularService.getTotal();
        Integer pytotal = popularService.getYesterdayTotal();
        Integer pntotal = popularService.getNowTotal();
        //模型数量 目前为空
        //图片数量
        Integer imtotal = specimenService.getImageTotal();
        Integer imytotal = specimenService.getYesterdayImageTotal();
        Integer imntotal = specimenService.getNowImageTotal();

        map.put("total", stotal + itotal + atotal + dtotal + ptotal + imtotal);
        Integer yestotal = sytotal + iytotal + aytotal + dytotal + pytotal +  imytotal;
        Integer nowtotal = snowtotal + intotal + antotal + dntotal + pntotal + imntotal;
        map.put("totalUp", nowtotal == 0 ? 0 : nowtotal - yestotal);
        map.put("totalImage",imtotal);
        map.put("totalImageUp",imntotal == 0 ? imntotal : imntotal - imytotal);
        map.put("totalModel",0);
        map.put("totalModelUp",0);
        map.put("totalPopular", ptotal);
        map.put("totalPopularUp",pntotal == 0 ? pntotal : pntotal - imntotal);
        map.put("totalSpecimen",stotal);
        map.put("totalPlantImage",itotal);
        map.put("totalAtlas",atotal);
        map.put("totalDna",dtotal);

        return ResponseResult.SUCCESS().setData( map);
    }


    @GetMapping("getStatTrend")
    public ResponseResult getStatTrend(@RequestParam(value = "startTime",required = false) String startTime,
                                       @RequestParam(value = "endTime",required = false) String endTime){

        HashMap<String,Object> map = new HashMap<>();
        List<String> xaxis = new ArrayList<>();
        List<Stat> yaxis = new ArrayList<>();
        String[] legend = {"腊叶标本","原植物图库","图谱库","DNA序列库"};
        //标本
        List<Stat> stotal = specimenService.getTotalGroupByDate(startTime, endTime);
        stotal.forEach(stat -> {
            xaxis.add(stat.getName());
        });

        // 原植物图库
        List<Stat> itotal = imageService.getTotalGroupByDate(startTime, endTime);
        itotal.forEach(stat -> {
            xaxis.add(stat.getName());
        });

        //图谱库
        List<Stat> atotal = atlasService.getTotalGroupByDate(startTime, endTime);
        atotal.forEach(stat -> {
            xaxis.add(stat.getName());
        });

        //dna
        List<Stat> dtotal = dnaService.getTotalGroupByDate(startTime, endTime);
        dtotal.forEach(stat -> {
            xaxis.add(stat.getName());
        });

        Set<String> set = new HashSet<>(xaxis);
        // List<String> result = new ArrayList<>(set);
        List<String> result = TimeUtil.sortDate(new ArrayList<>(set));
        List<Double> list = new ArrayList<>();
        List<Double> list1 = new ArrayList<>();
        List<Double> list2= new ArrayList<>();
        List<Double> list3 = new ArrayList<>();
        if (result.size() <= 0){
            result = TimeUtil.getDayList();
            for (int i = 0; i < result.size() ; i++) {
                list.add(0.0);
                list1.add(0.0);
                list2.add(0.0);
                list3.add(0.0);
            }
        }else {

            for (int i = 0; i < result.size() ; i++) {
                list.add(ArrUtil.check(stotal, result.get(i)));
                list1.add(ArrUtil.check(itotal,result.get(i)));
                list2.add(ArrUtil.check(atotal,result.get(i)));
                list3.add(ArrUtil.check(dtotal,result.get(i)));
            }


        }
        Stat st = new Stat();
        st.setName(legend[0]);
        st.setData(list);
        yaxis.add(st);
        Stat st1 = new Stat();
        st1.setName(legend[1]);
        st1.setData(list1);
        yaxis.add(st1);
        Stat st2 = new Stat();
        st2.setName(legend[2]);
        st2.setData(list2);
        yaxis.add(st2);
        Stat st3 = new Stat();
        st3.setName(legend[3]);
        st3.setData(list3);
        yaxis.add(st3);

        map.put("xdata",result);
        map.put("ydata",yaxis);
        map.put("legnd",legend);
        return ResponseResult.SUCCESS().setData(map);
    }


    @GetMapping("getStatSearchFreq")
    public ResponseResult getStatSearchFreq(){
        return ResponseResult.SUCCESS().setData( loggerService.getGroupByParam());
    }

    @GetMapping("getStatFgs")
    public ResponseResult getStatFgs(){
        List<Stat> list = new ArrayList<>();
        Stat ftotal = specimenService.getFamilyTotal();
        Stat gtotal = specimenService.getGenusTotal();
        Stat stotal = specimenService.getSpeciesTotal();
        double total =  ftotal.getValue() + gtotal.getValue() + stotal.getValue();
        ftotal.setValue(ArrUtil.twoRound(ftotal.getValue() / total * 100));
        gtotal.setValue(ArrUtil.twoRound(gtotal.getValue() / total * 100));
        stotal.setValue(ArrUtil.twoRound(stotal.getValue() / total * 100));
        list.add(ftotal);
        list.add(gtotal);
        list.add(stotal);
        return ResponseResult.SUCCESS().setData(list);
    }

    @GetMapping("getStatIndex")
    public ResponseResult getStatIndex(){
        Integer total = specimenService.getTotal();
        Integer imageTotal = specimenService.getImageTotalByType(0, 0);
        Integer videoTotal = specimenService.getImageTotalByType(1, 0);
        HashMap<String,Integer> map = new HashMap<>();
        map.put("total",total);
        map.put("modelTotal",0);
        map.put("imageTotal",imageTotal);
        map.put("videoTotal",videoTotal);
        return ResponseResult.SUCCESS().setData(map);
    }

    @GetMapping("getDayList")
    public ResponseResult getDayList(){
        return ResponseResult.SUCCESS().setData(TimeUtil.getDayList());
    }
}
